package com.zzw;

import com.zzw.model.QueryStuResponse;
import com.zzw.model.BaseStudent;
import com.zzw.service.StudentManagerService;
import com.zzw.service.StudentManagerServiceImpl;
import com.zzw.utils.BizUtil;

import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * @author zzw
 * @version 1.0
 * @title ManagerMain
 * @description <description class purpose>
 * @datetime 2022-12-28 8:29 下午
 */
public class ManagerMain {
    private static final String QUIT_KEY = "q";

    public static void main(String[] args) {
        StudentManagerServiceImpl manager = new StudentManagerServiceImpl();
        System.out.print("---------------------欢迎来到学员管理系统---------------------");
        while (true) {
            System.out.println("\n1.根据学号查看学员信息\n2.添加学员\n3.查看所有学员信息\n");
            System.out.print("请输入编码选择对应功能(输入q退出): ");
            Scanner functionScanner = new Scanner(System.in);
            String funcCode = functionScanner.next();
            if (QUIT_KEY.equalsIgnoreCase(funcCode)) {
                break;
            }

            selectFunc(funcCode, manager);
            if (funExitTips("该程序", false)) {
                break;
            }
        }
    }

    /**
     * 功能退出提示
     *
     * @param tips     提示信息
     * @param isFunTip 是否功能
     * @return 是否退出 ture-是, false-否
     */
    private static boolean funExitTips(String tips, boolean isFunTip) {
        try {
            // 延迟1s出现提示信息
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // ignore
            Thread.currentThread().interrupt();
        }
        String tipsStr = isFunTip ? "「功能" + tips + "」" : tips;
        System.out.printf("%n>>>是否需要继续使用%s?(输入q退出): ", tipsStr);
        Scanner needExec = new Scanner(System.in);
        String code = needExec.next();
        return QUIT_KEY.equalsIgnoreCase(code);
    }

    /**
     * 选择功能
     *
     * @param funcCode 功能编号
     * @param manager  学生管理服务对象
     */
    private static void selectFunc(String funcCode, StudentManagerService manager) {
        String funcTitle = ">>>>>>>>>>>>>>>>>>>>>[功能%s]%s<<<<<<<<<<<<<<<<<<<<<%n";

        switch (funcCode) {
            case "1":
                System.out.printf(funcTitle, funcCode, "查看学员信息");
                do {
                    System.out.print("请输入学生号(如 0001): ");
                    Scanner stuNoScanner = new Scanner(System.in);
                    String stuNo = stuNoScanner.next();
                    QueryStuResponse result = manager.queryStudent(stuNo);
                    System.out.println(BizUtil.formatPrint(result));
                } while (!funExitTips(funcCode, true));
                break;
            case "2":
                System.out.printf(funcTitle, funcCode, "添加学员信息");
                do {
                    BaseStudent stuBase = new BaseStudent();
                    System.out.print("请输入新增的学生号(如 0001): ");
                    Scanner stuNoScanner = new Scanner(System.in);
                    String stuNo = stuNoScanner.next();
                    System.out.print("请输入新增的学生姓名(如 张三): ");
                    Scanner nameScanner = new Scanner(System.in);
                    String name = nameScanner.next();
                    System.out.print("请输入新增的学生性别(如 M-男, F-女, N-未知): ");
                    Scanner sexScanner = new Scanner(System.in);
                    String sex = sexScanner.next();
                    stuBase.setStuNo(stuNo);
                    stuBase.setStuName(name);
                    stuBase.setStuSex(sex);
                    try {
                        manager.addStudent(stuBase);
                    } catch (Exception e) {
                        System.err.println("新增学员异常: " + e.getMessage() + "!!!");
                    }
                } while (!funExitTips(funcCode, true));
                break;
            case "3":
                System.out.printf(funcTitle, funcCode, "查所有学员信息");
                do {
                    System.out.print("请输入查询页码(默认每页10条): ");
                    Scanner pageNoStr = new Scanner(System.in);
                    try {
                        int pageNo = pageNoStr.nextInt();
                        List<QueryStuResponse> resultList = manager.queryAllStudent(pageNo, 10);
                        System.out.println(BizUtil.formatPrintList(resultList));
                    } catch (Exception e) {
                        System.err.println("输入错误, 无法查询学员信息。异常为: " + e.getClass());
                    }
                } while (!funExitTips(funcCode, true));
                break;
            default:
                System.out.println("不支持的功能, 请重新选择\n");
                break;
        }
    }
}
